// my/wx.js
import {
  AccountModel
} from '../../../models/account'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
import {
  InvioceModel
} from '../../../models/invioce'
const invioceModel = new InvioceModel()
const payModel = new PayModel()
const accountmodel = new AccountModel()
const app = getApp()
let last
const format = new Format()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    btnDisabled: true, //提交按钮是否禁用
    paw: '',
    confirmPaw: '',
    openTicketType: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      paw: '',
      confirmPaw: '',
      openTicketType: app.globalData.openTicketType
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindKeyInput(e) {
    console.log(e)
    if (e.currentTarget.dataset.name == 'idno') {
      if (e.detail.value.length == 18) {
        this.data[e.currentTarget.dataset.name] = e.detail.value
      } else {
        this.data[e.currentTarget.dataset.name] = ''
        this.setData({
          btnDisabled: true,
        })
      }
    } else {
      var reg=/^\d+$/;
      if (reg.test(e.detail.value) ) {
        if (e.detail.value.length == 6) {
          this.data[e.currentTarget.dataset.name] = e.detail.value
        } else {
          this.data[e.currentTarget.dataset.name] = ''
          this.setData({
            btnDisabled: true,
          })
        }
      } else {
        this.setData({
          btnDisabled: true,
        })
        wx.showToast({
          type: 'none',
          content: '请输入数字密码',
          duration: 1000,
        });
      }
    }
    console.log(this.data[e.currentTarget.dataset.name])
    if (this.data.paw && this.data.confirmPaw ) {
      this.setData({
        btnDisabled: false,
      })
    }
  },
  
  getsetPaw() {
    if (this.data.paw != this.data.confirmPaw) {
      wx.showToast({
        type: 'none',
        content: '两次密码输入不一致',
        duration: 1000,
      });
      return
    }
    // let idnoCheck = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
    // if (!(idnoCheck.test(this.data.idno))) {
    //   wx.showToast({
    //     type: 'none',
    //     content: '请输入正确身份证号',
    //     duration: 1000,
    //   });
    //   return
    // }
    accountmodel.setPayPwd(this.data.confirmPaw).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        app.globalData.ifSetPaw = 1
        wx.alert({
          content: '设置支付密码成功',
          buttonText: '知道了',
          success: () => {
            wx.navigateBack()
          }
        }); 
      } else {
        if(res) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
          return
        }
        wx.showToast({
          type: 'none',
          content: '设置密码失败',
          duration: 1000,
        });
      }
    })
  },
  // 变更开票类型
  checkout(e) {
    const index = e.currentTarget.dataset.index
    if (index == this.data.openTicketType) {
      return
    } else {
      this.modifyOpenTicketType(index)
    }
  },
  // 变更开票类型
  modifyOpenTicketType(index) {
    invioceModel.modifyOpenTicketType(index).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'openTicketType': index
        })
      } else {
        if (res && res.msg) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: '变更开票类型失败',
            duration: 1000,
          });
        }
        
      }
      
    })
  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})